import * as THREE from 'three'
import Experience from './Experience'
import { EventEmitter } from 'events'
export default class Utils extends EventEmitter {
    constructor() {
        super();
        this.experience = new Experience();
        const axesHelper = new THREE.AxesHelper(100);
        const gridHelper = new THREE.GridHelper(1000, 500);
        // this.experience.scene.add(axesHelper)
        this.boxOffsetLeft = this.experience.outBox.offsetLeft
        this.boxOffsetTop = this.experience.outBox.offsetTop
        console.log('左边距', this.boxOffsetLeft);
        console.log('上边距', this.boxOffsetTop);

        window.addEventListener("mousemove", event=>{
            this.emit('raycasterMove', event)
        })
        window.addEventListener("click", event=>{
            this.emit('raycasterClick', event)
        })


    }

    // 消除three.js中对象 防止内存溢出
    static disposeGroup(group) {
        // .traverse方法递归遍历group的所有后代
        group.traverse(function (obj) {
            if (obj.type == 'Mesh' || obj.type == 'Line') {
                obj.geometry.dispose();
                obj.material.dispose();
            }
        })
        if (group.children.length) {
            group.children = []; //删除所有后代模型对象
        }
    }
    /**
     *  xz 二维数组转三维  y值为0 (俯视)
     * @param {*} pointArr 
     * @returns 
     */
    static pointArrToVector3(pointArr = []) {
        // ep :  pointArr = [ [100, 0], [100, -80] ]
        const arr = pointArr.map(item => {
            return new THREE.Vector3(item[0], 0, item[1])
        })
        return arr;
    }

    /**
     * vec3数组转换成普通数组 3个坐标一组
     * @param {*} vector3Arr 
     * @returns 
     */
    static vector3ArrToNormal(vector3Arr = []) {
        const arr = []
        vector3Arr.forEach(item => arr.push(item.x, item.y, item.z))
        return arr
    }

    /**
     * 射线检测
     * @param {*} event 事件上下文
     * @param {*} sizes 尺寸
     * @param {*} camera 相机
     * @param {*} meshArr 需要检测的网格模型数组
     */
    rayChoose(event, meshArr) {
        const sizes = this.experience.sizes
        const camera = this.experience.camera.perspectiveCamera
        const Sx = event.clientX - this.boxOffsetLeft; //鼠标单击位置横坐标 210是由于有侧边栏
        const Sy = event.clientY - this.boxOffsetTop; //鼠标单击位置纵坐标 10 是由于有顶部的padding
        //屏幕坐标转WebGL标准设备坐标
        const x = (Sx / sizes.width) * 2 - 1; //WebGL标准设备横坐标
        const y = -(Sy / sizes.height) * 2 + 1; //WebGL标准设备纵坐标

        // console.log(Sx, Sy);
        const raycaster = new THREE.Raycaster(); //创建一个射线投射器`Raycaster`
        //通过鼠标单击位置标准设备坐标和相机参数计算射线投射器`Raycaster`的射线属性.ray
        raycaster.setFromCamera(new THREE.Vector2(x, y), camera);
        //返回.intersectObjects()参数中射线选中的网格模型对象
        const intersects = raycaster.intersectObjects(meshArr, true);
        return intersects
    }

    // getOffsetLeft(dom) {
    //     let offsetLeft = 0;
    //     do {
    //         offsetLeft += dom.offsetLeft
    //         dom = dom.parentNode
    //     } while (dom.parentNode)
    //     return offsetLeft
    // }

}